<?php

class Basket {

	/**
	 *
	 * @var array[int]BasketProductObject
	 */
	protected $_products = array();

	public function updateProductCount($productId, $diff) {
		return ($diff > 0) ? $this->updateProductAdd($productId, $diff) : $this->decreaseProduct($productId, -$diff);
	}

	/**
	 *
	 * @param int $productId
	 * @param int $count
	 */
	public function setProductCount($productId, $count, $authCount) {
		$productId = (int) $productId;
		$count = (int) $count;

		if (!isset($this->_products[$productId])) {
			$this->addProduct($productId, $count, $authCount);
		}
		/*elseif ($count <= 0) {
			$this->delProduct($productId);
		}*/
		else {
			$count == 0 ? $count = 1 : $count;
			$this->_setProductCount($productId, $count);
		}
	}

	/**
	 *
	 * @param int $productId
	 * @param int $newCount
	 */
	protected function _setProductCount($productId, $newCount) {
		$row = ProductPeer::getInstance()->fetchById($productId);
		if (!$row) {
			throw new Exception('Product does not exist');
		}

		$fullCount = $row->getCount();
		/*if (!$fullCount) {
			throw new Exception_BasketNoProductsInStore('Данного продукта нет на складе');
		}*/

		$this->_products[$productId]->count = min($newCount, $fullCount);
	}

	public function increaseProduct($productId, $count = 1) {
		$productId = (int) $productId;
		$count = (int) $count;

		/*if ($count <= 0) {
			throw new Exception('Invalid count');
		}*/

		if (!isset ($this->_products[$productId])) {
			$this->addProduct($productId, $count);
			return;
		}

		$oldCount = $this->_products[$productId]->count;

		$this->_setProductCount($productId, $oldCount + $count);
	}

	public function decreaseProduct($productId, $count = 1) {
		$productId = (int) $productId;
		$count = (int) $count;

		/*if ($count <= 0) {
			throw new Exception('Invalid count');
		}*/

		if (!isset ($this->_products[$productId])) {
			return;
		}
		$product = $this->_products[$productId];

		$oldCount = $product->count;

		/*if ($count >= $oldCount) {
			$this->delProduct($productId);
			return;
		}*/

		$product->count = $oldCount - $count;
	}

	public function delProduct($productId) {
		$productId = (int) $productId;
		unset ($this->_products[$productId]);
	}

	public function addProduct($productId, $count = 1, $authCount = null) {
		$productId = (int) $productId;
		$count = (int) $count;
		
		/*if ($count <= 0) {
			throw new Exception('Invalid count');
		}*/
		/* @var $row ProductRow */
		$row = ProductPeer::getInstance()->fetchById($productId);
		if (!$row) {
			throw new Exception('Product does not exist');
		}
		/*elseif ($row->getCount() <= 0) {
			//throw new Exception('Product price is not positive');
		}*/

		$this->_products[$productId] = new BasketProduct($row, $count);
	}

	public function getProducts() {
		return $this->_products;
	}

	public function checkProduct($id) {
		$id = (int) $id;
		return isset($this->_products[$id]);
	}

	public static function getTotalCost($products = null) {
		$totalCost = 0;
		foreach ($products as $product) {
			$totalCost += $product->getCost();
		}

		return $totalCost;
	}
}